package com.intellagric.service.impl.module.loganalyse;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.LogAnalyzeJobMinuteAppendMapper;
import com.intellagric.pojo.LogAnalyzeJobMinuteAppend;
import com.intellagric.pojo.LogAnalyzeJobMinuteAppendExample;
import com.intellagric.service.module.loganalyse.LogAnalyzeJobMinuteAppendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2019/5/11
 * @Description: 日志分析每分钟的增量管理服务
 */
@Service
public class LogAnalyzeJobMinuteAppendServiceImpl implements LogAnalyzeJobMinuteAppendService {

    @Autowired
    private LogAnalyzeJobMinuteAppendMapper logAnalyzeJobMinuteAppendMapper;

    /**
    * 添加日志分析每分钟的增量信息
    * @param logAnalyzeJobMinuteAppend
    * @return int
    */
    public ResponseMessage addLogAnalyzeJobMinuteAppend(LogAnalyzeJobMinuteAppend logAnalyzeJobMinuteAppend) {
        if (logAnalyzeJobMinuteAppendMapper.insertSelective(logAnalyzeJobMinuteAppend) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }
    
    /**
     * 删除日志分析每分钟的增量信息
     * @param indexName
     * @return int
     */
    public ResponseMessage deleteLogAnalyzeJobMinuteAppend(String indexName) {
        LogAnalyzeJobMinuteAppendExample logAnalyzeJobMinuteAppendExample = new LogAnalyzeJobMinuteAppendExample();
        logAnalyzeJobMinuteAppendExample.createCriteria().andIndexnameEqualTo(indexName);
        if (logAnalyzeJobMinuteAppendMapper.deleteByExample(logAnalyzeJobMinuteAppendExample) != 0) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }
    
    /**
     * 通过indexName查询日志分析每分钟的增量
     * @param indexName
     * @return LogAnalyzeJobMinuteAppend
     */
    public LayuiDataGridResult queryLogAnalyzeJobMinuteAppendById(String indexName, Date startTime, Date endTime) {
        LogAnalyzeJobMinuteAppendExample logAnalyzeJobMinuteAppendExample = new LogAnalyzeJobMinuteAppendExample();
        logAnalyzeJobMinuteAppendExample.setOrderByClause("executeTime DESC");
        LogAnalyzeJobMinuteAppendExample.Criteria criteria = logAnalyzeJobMinuteAppendExample.createCriteria();
        criteria.andIndexnameEqualTo(indexName);
        if (startTime != null && endTime != null) {
            criteria.andExecutetimeBetween(startTime,endTime);
        }
        List<LogAnalyzeJobMinuteAppend> logAnalyzeJobMinuteAppends = logAnalyzeJobMinuteAppendMapper.selectByExample(logAnalyzeJobMinuteAppendExample);
        return LayuiDataGridResult.success().add(logAnalyzeJobMinuteAppends,logAnalyzeJobMinuteAppends.size());
    }



}
